//
//  ViewController.m
//  UI22-QQ音乐MV
//
//  Created by 韩旭 on 16/8/12.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "MvlistModle.h"
#import "ListModel.h"
#import "SelfSizeTools.h"
#import "HX_CustomCell.h"
#import "HX_CycleImage.h"
#import "MV_ShowModel.h"
#define kTableView @"table"
@class MV_Data,MV_show_content,MV_liST,MV_LIST_Data,MV_Singer,Mv_Show;
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *arrTitleName;
@property (nonatomic, strong) NSMutableArray *arrDataSource;
@property (nonatomic, strong) UIView *viewHead;
@property (nonatomic, strong) NSMutableArray *arrCycleImage;
@property (nonatomic, strong) HX_CycleImage *hx_CycleImaeg;


@end

@implementation ViewController
- (NSMutableArray *)arrTitleName {
    if (!_arrTitleName) {
        _arrTitleName = [NSMutableArray array];
    }
    return _arrTitleName;
}
- (NSMutableArray *)arrDataSource {
    if (!_arrDataSource) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}
- (NSMutableArray *)arrCycleImage {
    if (!_arrCycleImage) {
        _arrCycleImage = [NSMutableArray array];
    }
    return _arrCycleImage;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"QQ音乐MV";
    [self getNetWorkingModel];
    [self creatTitleView];
    [self creatTableView];
}

- (void)creatTitleView {
    self.viewHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 300)];
    _viewHead.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_viewHead];
    self.hx_CycleImaeg = [[HX_CycleImage alloc] initWithFrame:CGRectMake(0, 0, kWidth, 200)];
    [self.viewHead addSubview:_hx_CycleImaeg];
    NSArray *arr = @[@"榜单", @"现场", @"频道"];
    NSArray *arrImage = @[@"bangdan", @"xianchang", @"pindao"];
    for (int i = 0; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * kWidth / 3, _hx_CycleImaeg.frame.size.height, kWidth / 3, _viewHead.frame.size.height - _hx_CycleImaeg.frame.size.height);
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:20];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:arrImage[i]] forState:UIControlStateNormal];
        btn.imageEdgeInsets = UIEdgeInsetsMake(35, 30, 35, 70);
        [self.viewHead addSubview:btn];
    }
}

- (void)creatTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStylePlain];
    _tableView.allowsMultipleSelection = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _viewHead;
    [self.view addSubview:_tableView];
    _tableView.sectionHeaderHeight = 50;
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HX_CustomCell class]) bundle:nil] forCellReuseIdentifier:kTableView];
}
- (void)getNetWorkingModel {
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    NSMutableSet *set = [NSMutableSet setWithObject:manger.responseSerializer.acceptableContentTypes];
    [set addObject:@"application/x-javascript"];
    [manger.responseSerializer setAcceptableContentTypes:set];
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manger GET:NETWORKING parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *str = [string substringFromIndex:7];
        NSString *strNew = [str substringToIndex:str.length - 1];
        NSData *data = [strNew dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSDictionary *dicData = dic[@"data"];
        NSDictionary *dicContents = dicData[@"contents"];
        NSArray *arrList = dicContents[@"list"];
        for (NSDictionary *dic in arrList) {
            [self.arrTitleName addObject:[ListModel BaseModelDic:dic]];
        }
        NSArray *arrMv_Show_Play = dicData[@"mv_show_play"];
        for (NSDictionary *dicMvShow in arrMv_Show_Play) {
     MV_ShowModel *mvShowModel = [MV_ShowModel BaseModelDic:dicMvShow];
            [self.arrCycleImage addObject:mvShowModel.picurl];
        }
        NSString *firstImage = [_arrCycleImage firstObject];
        NSString *lastImage = [_arrCycleImage lastObject];
        [_arrCycleImage insertObject:lastImage atIndex:0];
        [_arrCycleImage addObject:firstImage];
  _hx_CycleImaeg.arrDataSource = _arrCycleImage;
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 0)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
    CGRect frame = lable.frame;
    frame.size.width = [SelfSizeTools selfSizeHeightWithString:[_arrTitleName[section] name] Width:50 Font:[UIFont systemFontOfSize:20]];
    lable.font = [UIFont systemFontOfSize:20];
    
    lable.frame = frame;
    lable.center = CGPointMake(kWidth / 2, lable.frame.size.height / 2);
    lable.textAlignment = 0;
    lable.text = [_arrTitleName[section] name];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    image.center = CGPointMake(kWidth * 0.95, lable.frame.size.height / 2);
    image.image = [UIImage imageNamed:@"bofang"];
    [view addSubview:image];
    [view addSubview:lable];
    
    return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _arrTitleName.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HX_CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView];
    ListModel *list = _arrTitleName[indexPath.section];
    cell.arrDataSource = [list.mvlist mutableCopy];
    cell.mvlistModel = list.mvlist[indexPath.row];

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 500;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
@interface MV_Data : NSObject

@property (nonatomic, strong) NSArray<Mv_Show *> *mv_show_play;

@property (nonatomic, strong) MV_show_content *contents;

@end

@interface MV_show_content : NSObject

@property (nonatomic, strong) NSArray<MV_liST *> *list;

@end

@interface MV_liST : NSObject

@property (nonatomic, assign) NSInteger sum;

@property (nonatomic, copy) NSString *type;

@property (nonatomic, copy) NSString *jumpurl;

@property (nonatomic, copy) NSString *lan;

@property (nonatomic, strong) NSArray<MV_LIST_Data *> *mvlist;

@property (nonatomic, copy) NSString *name;

@end

@interface MV_LIST_Data : NSObject

@property (nonatomic, copy) NSString *singername;

@property (nonatomic, assign) NSInteger mvscore;

@property (nonatomic, copy) NSString *picurl;

@property (nonatomic, copy) NSString *publictime;

@property (nonatomic, copy) NSString *singer_name;

@property (nonatomic, copy) NSString *singer_mid;

@property (nonatomic, assign) NSInteger listennum;

@property (nonatomic, assign) NSInteger mv_id;

@property (nonatomic, strong) NSArray<MV_Singer *> *singers;

@property (nonatomic, copy) NSString *vid;

@property (nonatomic, assign) NSInteger singerid;

@property (nonatomic, copy) NSString *pub_date;

@property (nonatomic, copy) NSString *mvtitle;

@property (nonatomic, assign) NSInteger singer_id;

@property (nonatomic, copy) NSString *mvdesc;

@property (nonatomic, copy) NSString *singermid;

@end

@interface MV_Singer : NSObject

@property (nonatomic, assign) NSInteger id;

@property (nonatomic, copy) NSString *mid;

@property (nonatomic, copy) NSString *name;

@end

@interface Mv_Show : NSObject

@property (nonatomic, copy) NSString *picurl;

@property (nonatomic, copy) NSString *mvvid;

@property (nonatomic, assign) NSInteger playid;

@property (nonatomic, copy) NSString *subtitle;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, assign) NSInteger type;

@property (nonatomic, copy) NSString *playurl;

@end

